<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class inventory_edit extends Page
{
       public $edDesc = null;
       public $uploadImg = null;
       public $lblPhoto = null;
       public $lblcost = null;
       public $lblqty = null;
       public $lbldesc = null;
       public $lblpart = null;
       public $lblstock = null;
       public $Label12 = null;
       public $JSValidate = null;
       public $hfError = null;
       public $JTDivManufacturer = null;
       public $JTGroupBox2 = null;
       public $btnAddManufacturer = null;
       public $btnDelManufacturer = null;
       public $edManufacturer = null;
       public $listManufacturer = null;
       public $JTDivProducts = null;
       public $JTGroupBox1 = null;
       public $btnAddProducts = null;
       public $btnDelProducts = null;
       public $edProducts = null;
       public $listProducts = null;
       public $JTDivMeasure = null;
       public $JTGroupBox3 = null;
       public $btnAddMeasure = null;
       public $btnDelMeasure = null;
       public $edMeasure = null;
       public $listMeasure = null;
       public $Label8 = null;
       public $Label10 = null;
       public $cmbPrimaryVend = null;
       public $Label2 = null;
       public $edCost = null;
       public $Label3 = null;
       public $edPrice = null;
       public $edQtyAvail = null;
       public $btnShowManufacturer = null;
       public $cmbMnfr = null;
       public $Label5 = null;
       public $Label7 = null;
       public $btnShowMeasure = null;
       public $btnShowProducts = null;
       public $edBin = null;
       public $edRow = null;
       public $edAisle = null;
       public $Label9 = null;
       public $Label6 = null;
       public $cmbUOM = null;
       public $Label4 = null;
       public $chkinactive = null;
       public $cmbInvType = null;
       public $edPartNum = null;
       public $Label1 = null;
       public $edStockNUm = null;
       public $lblVendorNum = null;
       public $JTJavaScript9 = null;
       public $JTJavaScript8 = null;
       public $JTJavaScript7 = null;
       public $JTSiteTheme1 = null;
       public $JTJavaScript6 = null;
       public $JTJavaScript5 = null;
       public $JTJavaScript4 = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $imgBack = null;
       public $hiddenStock = null;
       public $Image1 = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       function edCostJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //accept only numbers
       var numero = document.getElementById("edCost").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edCost").value = numero;
       <?php

       }

       function edCostJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edCost','lblcost');
       <?php

       }


       function edQtyAvailJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edQtyAvail','lblqty');
       <?php

       }


       function edDescJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edDesc','lbldesc');
       <?php

       }

       function edPartNumJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edPartNum','lblpart');
       <?php

       }

       function edStockNUmJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edStockNUm','lblstock');
       <?php

       }

       function btnDelMeasureJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var MeasureIndex = document.getElementById('listMeasure').selectedIndex;
       var MeasureValue = document.getElementById('listMeasure')[MeasureIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxMeasure3(MeasureValue,comp);
       <?php

       }

       function btnAddMeasureJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addmeasure = document.getElementById('edMeasure').value;
       var comp = document.getElementById('compid').value;
       AjaxMeasure2(addmeasure,comp);
       document.getElementById('edMeasure').value = "";
       <?php

       }

       function btnDelManufacturerJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var MnfrIndex = document.getElementById('listManufacturer').selectedIndex;
       var MnfrValue = document.getElementById('listManufacturer')[MnfrIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxMnfr3(MnfrValue,comp);
       <?php

       }

       function btnAddManufacturerJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addmnfr = document.getElementById('edManufacturer').value;
       var comp = document.getElementById('compid').value;
       AjaxMnfr2(addmnfr,comp);
       document.getElementById('edManufacturer').value = "";
       <?php

       }

       function btnDelProductsJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var ProductsIndex = document.getElementById('listProducts').selectedIndex;
       var ProductsValue = document.getElementById('listProducts')[ProductsIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxProducts3(ProductsValue,comp);
       <?php

       }

       function btnAddProductsJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addproducts = document.getElementById('edProducts').value;
       var comp = document.getElementById('compid').value;
       AjaxProducts2(addproducts,comp);
       document.getElementById('edProducts').value = "";
       <?php

       }

       function btnShowMeasureJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivMeasure').Show();
       document.getElementById('edMeasure').value = "";
       <?php

       }

       function btnShowManufacturerJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivManufacturer').Show();
       document.getElementById('edManufacturer').value = "";
       <?php

       }

       function btnShowProductsJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivProducts').Show();
       document.getElementById('edProducts').value = "";
       <?php

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('inventory_list.php?stock_id='.$this->hiddenStock->getValue());


       }

       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }


       function inventory_editCreate($sender, $params)
       {
       if($_SESSION['access_part']==1)
       {
       $this->btnSubmit->setVisible(false);
       $this->btnCancel->setVisible(false);
       }

       if($_SESSION['access_part']==2)
       {
       $this->btnSubmit->setVisible(true);
       $this->btnCancel->setVisible(true);
       }

       $this->hfError->setValue('0');
       $this->lblstock->Caption = '';
       $this->lblpart->Caption = '';
       $this->lbldesc->Caption = '';
       $this->lblqty->Caption = '';
       $this->lblcost->Caption = '';

       $this->compid->Value = $_SESSION['company_id'];
       $stock_id = $_GET['stock_id'];
       $this->hiddenStock->setValue($stock_id);


       $query1 = "select * from invstocks_tbl where stock_id='".$this->hiddenStock->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);

       $this->edStockNUm->Text = $row['stock_num'];
       $this->edPartNum->Text = $row['part_num'];
       $this->edCost->Text = $row['cost'];
       $this->edDesc->Text = $row['description'];
       $this->edAisle->Text = $row['aisle'];
       $this->edRow->Text = $row['row'];
       $this->edBin->Text = $row['bin'];
       $this->edPrice->Text = $row['selling_price'];
       $this->edQtyAvail->Text = $row['qty_available'];


       $this->cmbInvType->Clear();
       $this->cmbInvType->AddItem('--Select--', null, null);
       $this->cmbInvType->ItemIndex=$row['type'];
        $result_type = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_type = mysql_fetch_array($result_type))
        {
            $this->cmbInvType->AddItem($row_type['value'], null, $row_type['value']);
        }

         //vendor
         $this->cmbPrimaryVend->Clear();
        $this->cmbPrimaryVend->AddItem('--Select--', null, null);
        $this->cmbPrimaryVend->ItemIndex=$row['vendor'];
        $result_vend = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

        while($row_vend = mysql_fetch_array($result_vend))
        {
            $this->cmbPrimaryVend->AddItem($row_vend['vendor_name'], null, $row_vend['vendor_name']);
        }


        //list products and services
        $result_products = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_products = mysql_fetch_array($result_products))
        {
            $this->listProducts->AddItem($row_products['value'], null, $row_products['value']);
        }

       $this->cmbMnfr->Clear();
       $this->cmbMnfr->AddItem('--Select--', null, null);
       $this->cmbMnfr->ItemIndex=$row['manufacturer'];
        $result_mnfr = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='manufacturers'");

        while($row_mnfr = mysql_fetch_array($result_mnfr))
        {
            $this->cmbMnfr->AddItem($row_mnfr['value'], null, $row_mnfr['value']);
        }

         //list manufacturer
        $result_manufacturer = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='manufacturers'");

        while($row_manufacturer = mysql_fetch_array($result_manufacturer))
        {
            $this->listManufacturer->AddItem($row_manufacturer['value'], null, $row_manufacturer['value']);
        }


        $this->cmbUOM->Clear();
        $this->cmbUOM->AddItem('--Select--', null, null);
        $this->cmbUOM->ItemIndex=$row['uof'];
        $result_uom = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='unit of measure'");

        while($row_uom = mysql_fetch_array($result_uom))
        {
            $this->cmbUOM->AddItem($row_uom['value'], null, $row_uom['value']);
        }

        //list measure
        $result_measure = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='unit of measure'");

        while($row_measure = mysql_fetch_array($result_measure))
        {
            $this->listMeasure->AddItem($row_measure['value'], null, $row_measure['value']);
        }

       $this->imgBack->setLink('inventory_list.php?stock_id='.$this->hiddenStock->getValue());

       }

       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edDesc->Text, 'lbldesc');
       $this->validateRequired($this->edPartNum->Text, 'lblpart');
       $this->validateRequired($this->edStockNUm->Text, 'lblstock');
       $this->validateRequired($this->edQtyAvail->Text, 'lblqty');
       $this->validateRequired($this->edCost->Text, 'lblcost');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       if($this->chkinactive->Checked==true)
       {
       $inactive = '1';
       }
       else
       {
       $inactive = '0';
       }

        $price = $this->edPrice->Text;
        $cost = $this->edCost->Text;
        $salvage_price = $this->commafy($price);
        $part_cost = $this->commafy($cost);

       $result_inv = mysql_query("SELECT * FROM invstocks_tbl where stock_id = '".$this->hiddenStock->getValue()."'") or die("SELECT INV ID - ".mysql_error());
       $row_inv = mysql_fetch_array($result_inv);

       if($this->uploadImg->FileExt=="")
       {
       $query_update = "UPDATE invstocks_tbl SET
       stock_num ='".$this->edStockNUm->Text."',
       part_num ='".$this->edPartNum->Text."',
       type ='".$this->cmbInvType->getItemIndex()."',
       description ='".$this->edDesc->Text."',
       manufacturer ='".$this->cmbMnfr->getItemIndex()."',
       uof ='".$this->cmbUOM->getItemIndex()."',
       cost ='".$part_cost."',
       aisle = '".$this->edAisle->Text."',
       row = '".$this->edRow->Text."',
       bin = '".$this->edBin->Text."',
       selling_price ='".$salvage_price."',
       vendor = '".$this->cmbPrimaryVend->getItemIndex()."',
       inactive ='".$inactive."',
       qty_available = '".$this->edQtyAvail->Text."'
       where stock_id='".$this->hiddenStock->getValue()."'";
       mysql_query($query_update) or die(mysql_error());
       }

       if($this->uploadImg->FileExt!="")
       {
       $query_update = "UPDATE invstocks_tbl SET
       stock_num ='".$this->edStockNUm->Text."',
       part_num ='".$this->edPartNum->Text."',
       type ='".$this->cmbInvType->getItemIndex()."',
       description ='".$this->edDesc->Text."',
       manufacturer ='".$this->cmbMnfr->getItemIndex()."',
       uof ='".$this->cmbUOM->getItemIndex()."',
       cost ='".$part_cost."',
       aisle = '".$this->edAisle->Text."',
       row = '".$this->edRow->Text."',
       bin = '".$this->edBin->Text."',
       selling_price ='".$salvage_price."',
       vendor = '".$this->cmbPrimaryVend->getItemIndex()."',
       inactive ='".$inactive."',
       qty_available = '".$this->edQtyAvail->Text."',
       photo ='"."photo/inventory/inv".$this->hiddenStock->getValue().".".$this->uploadImg->FileExt."'
       where stock_id='".$this->hiddenStock->getValue()."'";
       mysql_query($query_update) or die(mysql_error());
       }

       $this->uploadImg->moveUploadedFile("photo/inventory/inv".$this->hiddenStock->getValue().".".$this->uploadImg->FileExt);
       redirect('inventory_list.php?stock_id='.$this->hiddenStock->getValue());
       }
       }

}

global $application;

global $inventory_edit;

//Creates the form
$inventory_edit=new inventory_edit($application);

//Read from resource file
$inventory_edit->loadResource(__FILE__);

//Shows the form
$inventory_edit->show();

?>